package com.keqiang.chart.impl.axis.impl.bar

import android.graphics.RectF
import com.keqiang.chart.impl.axis.BaseTouchHandler

/**
 * 处理柱状图的触摸
 *
 * @author Created by wanggaowan on 2022/7/29 14:06
 */
open class BarTouchHandler(chart: BarChart) : BaseTouchHandler<BarChart, List<BarEntity>>(chart) {

    override fun getTouchData(x: Float, y: Float, isMove: Boolean): List<BarEntity>? {
        if (chart.data == null) {
            return null
        }

        val inRangePoint = mutableListOf<BarEntity>()
        chart.data?.groups?.forEach {
            for (value in it.values) {
                if (isTouchInRectF(x, y, value.drawRectF, isMove)) {
                    inRangePoint.add(value)
                    break
                }
            }
        }

        return if (inRangePoint.isEmpty()) null else inRangePoint
    }

    /**
     * 点击是否在指定范围内容，[isYAllValid]表示是否y轴全局域触摸有效，如果为true，那么则无需判断手指y轴坐标
     */
    private fun isTouchInRectF(x: Float, y: Float, rectF: RectF, isYAllValid: Boolean = false): Boolean {
        if (x >= rectF.left && x <= rectF.right) {
            if (isYAllValid) {
                return true
            }

            return y >= rectF.top && y <= rectF.bottom
        }

        return false
    }
}